# Maintainer: Ryuta Suzuki <oroppas@gmail.com>

_realname=xlsxwriter
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=1.2.2
pkgrel=1
pkgdesc="A Python module for creating Excel XLSL files (mingw-w64)"
url="https://github.com/jmcnamara/XlsxWriter"
arch=('any')
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-pytest"
              "${MINGW_PACKAGE_PREFIX}-python2-pytest")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/jmcnamara/XlsxWriter/archive/RELEASE_${pkgver}.tar.gz")
sha256sums=('227279fa4389808b8b894830dfc4e0ae3f3055e81767677a24ee8da03527a98e')

prepare() {
  cd "${srcdir}"
  
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "XlsxWriter-RELEASE_${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

#check() {
#  cd "${srcdir}/python3-build-${CARCH}"
#  py.test-3.7.exe
#
#  cd "${srcdir}/python2-build-${CARCH}"
#  py.test-2.7
#}

package_python3-xlsxwriter() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for filename in vba_extract; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py "${pkgdir}${MINGW_PREFIX}"/bin/${filename}-py3.py
  done
}

package_python2-xlsxwriter() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for filename in vba_extract; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py
  done
}

package_mingw-w64-i686-python2-xlsxwriter() {
  package_python2-xlsxwriter
}

package_mingw-w64-i686-python3-xlsxwriter() {
  package_python3-xlsxwriter
}

package_mingw-w64-x86_64-python2-xlsxwriter() {
  package_python2-xlsxwriter
}

package_mingw-w64-x86_64-python3-xlsxwriter() {
  package_python3-xlsxwriter
}

# vim:set ts=2 sw=2 et:
